<?php

//首先分割,将所有的有效域名分割成不同的小文件,理论上平均一个域名需要300s的时间去发外链
//程序要保证每天每个域名发4次外链,一个文件要含有100个域名
require_once dirname(__FILE__) . '/autoload.php';
$s = file_get_contents('http://tools.u631.com/onlinecheck/locklist_allinone.php');
if ($s) {
    file_put_contents(dirname(__FILE__) . '/locklist_allinone.php', $s);
}

$_g_runtype = $argv[1];
if (!$_g_runtype) {
    die('please enter $_g_runtype!' . PHP_EOL);
}

if (!in_array($_g_runtype, array('jiankong', 'wailian'))) {
    die('$_g_runtype is error!' . PHP_EOL);
}


$s = '';
if (file_exists(dirname(__FILE__) . '/domainlist.txt')) {
    $s = file_get_contents(dirname(__FILE__) . '/domainlist.txt');
}

$domainlist = array_filter(explode('<br />', nl2br(trim($s))));
foreach ($domainlist as &$ds) {
    $ds = preg_replace('#\s+#', '', $ds);
}unset($ds);
$domainlist = array_filter($domainlist);


$s = '';
if (file_exists(dirname(dirname(__FILE__)) . '/stat/domainlist.txt')) {
    $s = file_get_contents(dirname(__FILE__) . '/stat/domainlist.txt');
}

$tdomainlist = array_filter(explode('<br />', nl2br(trim($s))));
foreach ($tdomainlist as &$ds) {
    $ds = preg_replace('#\s+#', '', $ds);
}unset($ds);
$tdomainlist = array_filter($tdomainlist);

$domainlist = array_merge($domainlist, $tdomainlist);

$s = '';
if (file_exists(dirname(__FILE__) . '/stat/tkdomain.txt')) {
    $s = file_get_contents(dirname(__FILE__) . '/stat/tkdomain.txt');
}

$tdomainlist = array_filter(explode('<br />', nl2br(trim($s))));
foreach ($tdomainlist as &$ds) {
    $ds = preg_replace('#\s+#', '', $ds);
}unset($ds);
$tdomainlist = array_filter($tdomainlist);

$domainlist = array_merge($domainlist, $tdomainlist);

$locallockfile = dirname(__FILE__) . '/locklist_allinone.php';
if (!file_exists($locallockfile)) {
    log::e('!file_exists($locallockfile)', true, true);
    die();
}
$lockdomains = json_decode(file_get_contents($locallockfile), true);
$arrUniqDomainDataUniq = array();
$arrUniqDomainDataWWW = array_merge($domainlist, $lockdomains);

foreach ($arrUniqDomainDataWWW as $domainALL) {
    $arrDInfo = explode('.', $domainALL);
    $length = count($arrDInfo);
    $extk = $length - 1;
    $dok = $length - 2;
    $domain = $arrDInfo[$dok] . '.' . $arrDInfo[$extk];
    if (is_numeric($arrDInfo[$extk])) {
        log::w($domain);
        continue;
    }
    $arrUniqDomainDataUniq[] = $domainALL;
}

$arrUniqDomainDataUniq = array_filter($arrUniqDomainDataUniq);
$arrUniqDomainDataUniq = array_unique($arrUniqDomainDataUniq);
sort($arrUniqDomainDataUniq);
$TotalCount = count($arrUniqDomainDataUniq);
$arrDomain100Data = array();

for ($i = 0; $i < $TotalCount; $i++) {
    $fileflag = ceil($i / 700);
    $arrDomain100Data[$fileflag][] = $arrUniqDomainDataUniq[$i];
}

foreach ($arrDomain100Data as $fileflag => $arrDomainList) {
    $domainfile = dirname(__FILE__) . '/s100-domain-' . $fileflag . '.php';
    file_put_contents($domainfile, json_encode($arrDomainList));
}

if ($_g_runtype == 'jiankong') {
    
}

if ($_g_runtype == 'wailian') {
    foreach ($arrDomain100Data as $fileflag => $arrDomainList) {
        for ($iIndex = 0; $iIndex <= 5; $iIndex++) {
            $phpcmd = "nohup php bin.sendwl.php {$fileflag} {$iIndex} > send-{$fileflag}-{$iIndex}.log 2>&1 &";
            log::n($phpcmd);
            shell_exec($phpcmd);
        }
    }
}
?>